<template>
  <div class="pageDetail">
    <el-form
      ref="formRef"
      class="form_item_w2"
      :model="pageForm"
      :rules="rules"
      label-width="130px"
    >
      <el-form-item label="教师姓名" prop="teacherName">
        <el-input v-model="pageForm.teacherName" placeholder="请输入教师姓名" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="评价周期" prop="evaluationPeriod">
        <el-input v-model="pageForm.evaluationPeriod" placeholder="请输入评价周期" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="所在部门" prop="deptId">
        <TheDeptSelect v-model="pageForm.deptId" :modelValue="pageForm.deptId" :deptType="42" placeholder="请选择所在部门"  :disabled="disabled" @update:modelValue="pageForm.deptId = $event"/>
      </el-form-item>
      <el-form-item label="实训教学设计" prop="scoreA">
        <el-input-number v-model="pageForm.scoreA" :min="0" label="请输入1-10分" :disabled="disabled" @change="calculateTotalScore()"></el-input-number>
      </el-form-item>
      <el-form-item label="教学目标达成度" prop="scoreB">
        <el-input-number v-model="pageForm.scoreB" :min="0" label="请输入1-10分" :disabled="disabled" @change="calculateTotalScore()"></el-input-number>
      </el-form-item>
      <el-form-item label="作业批改质量" prop="scoreC">
        <el-input-number v-model="pageForm.scoreC" :min="0" label="请输入1-10分" :disabled="disabled" @change="calculateTotalScore()"></el-input-number>
      </el-form-item>
      <el-form-item label="师德师风表率" prop="scoreD">
        <el-input-number v-model="pageForm.scoreD" :min="0" label="请输入1-10分" :disabled="disabled" @change="calculateTotalScore()"></el-input-number>
      </el-form-item>
      <el-form-item label="学生课堂表现" prop="scoreE">
        <el-input-number v-model="pageForm.scoreE" :min="0" label="请输入1-10分" :disabled="disabled" @change="calculateTotalScore()"></el-input-number>
      </el-form-item>
      <el-form-item label="技能证书考取" prop="scoreF">
        <el-input-number v-model="pageForm.scoreF" :min="0" label="请输入1-10分" :disabled="disabled" @change="calculateTotalScore()"></el-input-number>
      </el-form-item>
      <el-form-item label="家长沟通频率" prop="scoreG">
        <el-input-number v-model="pageForm.scoreG" :min="0" label="请输入1-10分" :disabled="disabled" @change="calculateTotalScore()"></el-input-number>
      </el-form-item>
      <el-form-item label="教学资源开发" prop="scoreH">
        <el-input-number v-model="pageForm.scoreH" :min="0" label="请输入1-10分" :disabled="disabled" @change="calculateTotalScore()"></el-input-number>
      </el-form-item>
      <el-form-item label="企业实践参与" prop="scoreI">
        <el-input-number v-model="pageForm.scoreI" :min="0" label="请输入1-10分" :disabled="disabled" @change="calculateTotalScore()"></el-input-number>
      </el-form-item>
      <el-form-item label="教研活动参与" prop="scoreJ">
        <el-input-number v-model="pageForm.scoreJ" :min="0" label="请输入1-10分" :disabled="disabled" @change="calculateTotalScore()"></el-input-number>
      </el-form-item>
      <el-form-item label="综合评分" prop="totalScore">
        <el-input-number v-model="pageForm.totalScore" :min="0" label="系统自动计算" :disabled="true"></el-input-number>
      </el-form-item>
      <el-form-item label="教学优势总结" class="item_w1">
        <el-input type="textarea" v-model="pageForm.strengthsSummary" placeholder="请输入教学优势总结" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="改进计划" class="item_w1">
        <el-input type="textarea" v-model="pageForm.improvementPlan" placeholder="请输入改进计划" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="待改进问题" class="item_w1">
        <el-input type="textarea" v-model="pageForm.improvementAreas" placeholder="请输入待改进问题" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="未来发展目标" class="item_w1">
        <el-input type="textarea" v-model="pageForm.developmentGoals" placeholder="请输入未来发展目标" :disabled="disabled"></el-input>
      </el-form-item>
    </el-form>
    <div class="pageFooterBtn">
      <el-button @click="$router.back()">返回</el-button>
      <el-button type="primary" @click="handleSubmit()" v-if="['add','edit'].includes(action)" :loading="loading">提交</el-button>
    </div>
  </div>
</template>
<script>
import { rules } from './data'
import { evaluationAddApi, evaluationEditApi, evaluationByIdApi } from '@/api/intelligentEvaluation/index'
import FileUpload from "@/components/FileUpload";
import { getInfo } from '@/api/login'
import TheDeptSelect from '@/componentsView/TheDeptSelect.vue';

export default {
  name: 'SupervisorDetail',
  components: { TheDeptSelect, FileUpload },
  data() {
    return {
      rules,
      userList: [],
      pageForm: {},
      loading:false,
      disabled:false,
       evaluationId: this.$route.query. evaluationId,
      action: this.$route.query.action
    }
  },

  async created() {
    this.getUserInfo();
    if (this. evaluationId) this.getCoursePlanById()
  },
  methods: {
    getUserInfo() {
      // 调用getInfo方法并处理返回的Promise
      getInfo().then(res => {
        this.pageForm = Object.assign({},this.pageForm, {
          teacherId:res.user.userId,
          teacherName:res.user.userName
        })
      });
    },
    async handleSubmit() {
      try {
        await this.$refs.formRef.validate()
        this.loading = true
        let res = {}
        this.pageForm.evaluationTypeId = '193'
        if (this. evaluationId) {
          delete this.pageForm.fileInfo
          res = await evaluationEditApi(this.pageForm)
        }else{
          res = await evaluationAddApi(this.pageForm)
        }
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$router.back()
        } else {
          this.$message.error(res.msg || '操作失败')
        }
      } catch (error) {
        this.$message.error('操作失败')
      } finally {
        this.loading = false
      }
    },

    async getCoursePlanById(){
      if (this.action === 'look') this.disabled = true
      evaluationByIdApi(this. evaluationId).then(res => {
        if (res.code === 200) {
          this.pageForm = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    calculateTotalScore() {
      // 定义参与计算的字段数组
      const scoreFields = [
        'scoreA',
        'scoreB',
        'scoreC',
        'scoreD',
        'scoreE',
        'scoreF',
        'scoreG',
        'scoreH',
        'scoreI',
        'scoreJ',
      ];

      // 过滤出已填写的有效分数（排除null/undefined）
      const validScores = scoreFields
        .map(field => this.pageForm[field])
        .filter(score => score !== null && score !== undefined);

      // 计算平均值（保留两位小数）
      if (validScores.length > 0) {
        const sum = validScores.reduce((total, score) => total + score, 0);
        this.pageForm.totalScore = sum;
      } else {
        // 若没有填写任何评分，综合得分为空
        this.pageForm.totalScore = null;
      }
    }
  },
}
</script>
<style scoped lang="scss">
::v-deep .el-select {
	width: 100% !important;
}
::v-deep .el-date-editor{
  width: 100% !important;
}
::v-deep .el-input-number{
  width: 100% !important;
}
</style>